import LF from 'leaflet'
L.PositionMarker = L.Marker.extend({
  options: {
    interactive: false
  },
  initialize: function(latlng, options) {
    options = options || {}
    options.icon = new L.Icon({
      iconUrl:
        '',
      iconSize: [36, 36],
      iconAnchor: [18, 18]
    })
    L.Marker.prototype.initialize.call(this, latlng, options)
  },
  onAdd: function(map) {
    L.Marker.prototype.onAdd.call(this, map)
    this.flashCount = 0
    this._startFlash()
  },
  onRemove: function(map) {
    L.Marker.prototype.onRemove.call(this, map)
    this._removed = true
    this._stopFlash()
  },
  _startFlash: function() {
    let that = this
    let flashFunc = function() {
      if (that._removed || that.flashCount > 3) {
        that._map.removeLayer(that)
        return
      }
      let icon = that.options.icon
      let { iconSize } = icon.options
      that.flashCount += iconSize[0] <= 24 ? 1 : 0
      let size = iconSize[0] <= 24 ? 36 : iconSize[0] - 4
      icon.options.iconSize = [size, size]
      icon.options.iconAnchor = [size / 2, size / 2]
      that.setIcon(icon)

      that._flashTimer = window.setTimeout(flashFunc, 300)
    }
    this._flashTimer = window.setTimeout(flashFunc, 300)
  },
  _stopFlash: function() {
    if (!this._flashTimer) {
      return
    }
    window.clearTimeout(this._flashTimer)
  }
})

let Position = {
  setCenterView: (map, center, zoom) => {
    setTimeout(function() {
      let latLng = LF.latLng(center)
      map._onResize() //路由转换时候导致地图不完整的bug——个别浏览器出现
      map.setView(latLng, zoom || 10)
    }, 300)
  },
  startPosition: function($v, map, positionMarkerId, vectorMarkers, zoom) {
    if (!positionMarkerId || positionMarkerId === '') {
      return
    }
    let marker = vectorMarkers[positionMarkerId]
    if (!marker) {
      return
    }

    let latLng = marker.getLatLng()
    setTimeout(function() {
      map._onResize() //路由转换时候导致地图不完整的bug——个别浏览器出现
      map.setView(latLng, zoom || 10)

      Position.closePosition($v, map)

      $v._positionMarker = new L.PositionMarker(latLng)
      map.addLayer($v._positionMarker)
    }, 500) //延迟500毫秒是为了使路由转换完成
    positionMarkerId = ''
  },
  closePosition: function($v, map) {
    if ($v._positionMarker) {
      map.removeLayer($v._positionMarker)
      delete $v._positionMarker
    }
  }
}
export default Position
